package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.entity.dto.SysProjectPageParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.mapper.SysProjectMapper;
import com.xinsoft.service.SysProjectService;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author hsm
* @description 针对表【sys_project(项目表)】的数据库操作Service实现
* @createDate 2023-09-06 14:40:29
*/
@Service
public class SysProjectServiceImpl extends ServiceImpl<SysProjectMapper, SysProject>
    implements SysProjectService {

    @Override
    public SysProject addSysProject(SysProject sysProjectDTO) {
        // 判断项目名称是否重复
        QueryWrapper<SysProject> sysProjectQuery = new QueryWrapper<>();
        sysProjectQuery.select("id");
        sysProjectQuery.eq("project_name",sysProjectDTO.getProjectName());
        List projects = this.list(sysProjectQuery);

        if(CollectionUtil.isNotEmpty(projects)){
            throw new BDException("操作失败，项目名称重复！");
        }

        //判断项目code是否重复
        QueryWrapper<SysProject> sysProjectQueryByCode = new QueryWrapper<>();
        sysProjectQueryByCode.select("id");
        sysProjectQueryByCode.eq("project_code",sysProjectDTO.getProjectCode());
        List projects2 = this.list(sysProjectQueryByCode);
        if(CollectionUtil.isNotEmpty(projects2)){
            throw new BDException("操作失败，项目编号重复！");
        }

        SysProject project = new SysProject();
        BeanUtils.copyProperties(sysProjectDTO,project);
        project.setAuditStatus(0);//未审核
        this.save(project);
        return project;

    }

    @Override
    public SysProject editSysProject(SysProject sysProjectDTO) {

        //判断是否可以修改//禁用状态且未审核
        SysProject project = this.getById(sysProjectDTO.getId());
        if(Objects.equals(project.getAuditStatus(),1)){
            throw new BDException("操作失败，项目已审核无法修改！");
        }

        // 判断项目名称是否重复
        QueryWrapper<SysProject> sysProjectQuery = new QueryWrapper<>();
        sysProjectQuery.select("id");
        sysProjectQuery.eq("project_name",sysProjectDTO.getProjectName());
        sysProjectQuery.ne("id",sysProjectDTO.getId());
        List projects = this.list(sysProjectQuery);

        if(CollectionUtil.isNotEmpty(projects)){
            throw new BDException("操作失败，项目名称重复！");
        }

        BeanUtils.copyProperties(sysProjectDTO,project);
        project.setProjectCode(null);
        project.setAuditStatus(0);//未审核
        // 保存项目
        this.updateById(project);

        return project;
    }

    @Override
    public void del(List<Integer> ids) {

        QueryWrapper<SysProject> sysUserQuery2 = new QueryWrapper<>();
        sysUserQuery2.eq("audit_status",1);//启用
        List<SysProject> projects2 = this.list(sysUserQuery2);
        //判断是否已启用
        if(CollectionUtil.isNotEmpty(projects2)){
            throw new BDException("操作失败，"+projects2.get(0).getProjectName()+"已审核无法删除！");
        }

        // 删除
        removeByIds(ids);
    }

    @Override
    public SysProject getSysProject(Integer id) {
        SysProject project = this.getById(id);
        return project;
    }

    @Override
    public void audit(List<Integer> ids) {
        UpdateWrapper<SysProject> sysUserUpdateQuery = new UpdateWrapper<>();
        sysUserUpdateQuery.set("audit_status",1);//审核
        sysUserUpdateQuery.set("audit_id", TokenUtil.getUserId());//设置审核人
        sysUserUpdateQuery.in("id",ids);
        sysUserUpdateQuery.eq("do_delete",0);
        this.update(sysUserUpdateQuery);
    }

    @Override
    public void deAudit(List<Integer> ids) {
        UpdateWrapper<SysProject> sysUserUpdateQuery = new UpdateWrapper<>();
        sysUserUpdateQuery.set("audit_status",0);//反审核
        sysUserUpdateQuery.set("audit_id", TokenUtil.getUserId());//设置审核人
        sysUserUpdateQuery.in("id",ids);
        sysUserUpdateQuery.eq("do_delete",0);
        this.update(sysUserUpdateQuery);
    }

    @Override
    public IPage<SysProject> findPageListByParam(SysProjectPageParam param) {
        //分页
        Page<SysProject> page = new Page<SysProject>(param.getCurrentPage(), param.getPageSize());
        QueryWrapper<SysProject> queryWrapper = new QueryWrapper<>();
        //查询条件
        if(StrUtil.isNotBlank(param.getProjectName())){
            queryWrapper.like("project_name",param.getProjectName());
        }
        IPage<SysProject> resultPage = this.baseMapper.selectPage(page, queryWrapper);

        return resultPage;
    }

}



